package models

import (
	"192.168.1.75/go-pkg/helper"
	"gorm.io/gorm"
)

// AuthRule 权限规则
type AuthRule struct {
	ID          string `json:"id" gorm:"column:id;primaryKey;type:int;size:64;comment:主键"`
	Title       string `json:"title" gorm:"column:title;type:string;size:40;comment:标题"`
	Name        string `json:"name" gorm:"column:name;type:string;size:40;index:idx_unique_name,unique;default:null;comment:名称"`
	Value       string `json:"value" gorm:"column:value;type:string;size:40;index:idx_unique_value,unique;default:null;comment:权限值"`
	RuleType    int    `json:"rule_type" gorm:"column:rule_type;type:int;size:8;index:idx_unique_value,unique;default:0;comment:规则类型,1-系统,2-菜单权限,3-操作权限,4-过滤"`
	PID         string `json:"pid" gorm:"column:pid;type:int;size:64;index:idx_unique_value,unique;index:idx_unique_name,unique;comment:上级id"`
	Apis        string `json:"-" gorm:"column:apis;type:TEXT;size:10000;comment:接口,多个以','分隔"`
	DBTableName string `json:"-" gorm:"column:db_table_name;type:string;size:40;comment:关联的数据表"`
	Order       int    `json:"order" gorm:"column:order;type:int;size:8;comment:排序"`
	CreatedAt   int64  `json:"created_at" gorm:"autoCreateTime;column:created_at;type:int;size:64;comment:创建时间"`
	UpdatedAt   int64  `json:"updated_at" gorm:"column:updated_at;type:int;size:64;comment:更新时间"`
	DeletedAt   int64  `json:"deleted_at" gorm:"column:deleted_at;type:int;size:64;index:idx_unique_name,unique;index:idx_unique_value,unique;default:null;comment:删除时间"`
}

// TableName 表名称
func (ar *AuthRule) TableName() string {
	return "auth_rule"
}

// BeforeCreate 自增id
func (ar *AuthRule) BeforeCreate(tx *gorm.DB) (err error) {
	uuid, err := helper.UUID{}.String()
	if err != nil {
		return err
	}
	ar.ID = uuid
	return nil
}

// 模式自动注册
func init() {
	Models = append(Models, &AuthRule{})
}
